/*
 * @Description  : 
 * @Version      : 1.0
 * @Author       : Shiming
 * @Date         : 2022-03-14 14:17:38
 * @LastEditors  : Shiming
 * @LastEditTime : 2022-03-25 13:52:54
 * @FilePath     : \\tms-obc-web\\src\\app\\routes\\passport\\components\\order-agreement\\order-agreement.component.ts
 * Copyright (C) 2022 huzhenhong. All rights reserved.
 */
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute, Params } from '@angular/router';
import { PassportService } from '../../services/passport.service';

@Component({
  selector: 'app-buyer-components-serve-order-agreement',
  templateUrl: './order-agreement.component.html',
  styleUrls: ['./order-agreement.component.less']
})
export class OrderAgreementComponent implements OnInit {
  agreementContent: any;
  type :any= 2;
  types :any;
  data : any;
  object: any;
  enterpriseInfo: any; // 网络货运人
  shipperName: any;


  constructor(private ar: ActivatedRoute, private service: PassportService) {
    ar.queryParams?.subscribe((params: Params) => {
      console.log(params);
      
      this.type = params?.type || 2;
      this.types = params?.types;
      this.object = JSON.parse(params?.object);
      this.shipperName = params?.shipperName;
      if(params?.data) {
        this.data = JSON.parse(params?.data) || {};
      }
    });
  }
  ngOnInit() {
    console.log(this.type);
    if(this.type == 3) {
      console.log(this.data);
      
      this.service.request(this.service.$api_get_getSupplementaryAgreement, { billId: this.data?.billId, billCode: this.data?.billCode }).subscribe(res => {
        if (res) {
          this.agreementContent = res;
        }
      });
    }else if(this.type == 15) {
         // 获取托运人承运人信息
    this.service.request(this.service.$api_getContractAtr, { id: this.object?.shipperAppUserId }).subscribe(res => {
      if (res) {
        this.enterpriseInfo = res;
        this.getContent(); 
      }
    });
    } else {
      this.service.request(this.service.$api_get_agreement, { type: this.type }).subscribe(res => {
        if (res) {
          this.agreementContent = res;
        }
      });
    }
  }
  
  getContent() {
    let params: any;
    console.log(this.types);
    if (this.types == 'onecar') {
      params = {
        contractType: '1',
        resourceType: '1',
        signingObject: '1',
        templateType: 'MX',
        parametersDTO: {
          contractCode: '',
          shipperLegalPersonName: this.enterpriseInfo.legalPersonName || '', //托运法定代表人
          carrierLegalPersonName: this.enterpriseInfo.netLegalPersonName || '', //承运法定代表人
          shipperName: this?.shipperName || '', //托运人
          carrierName: this.object?.enterpriseInfoNamer ||  this.object?.enterpriseInfoName || '', //承运人
          consignorInfo: `${this.object?.unLoadingPlaceDTOList[0]?.appUserName || ''}  ${
            this.object?.unLoadingPlaceDTOList[0]?.contractTelephone || ''
          }`, // 发货信息
          consignorAddress: this.object?.unLoadingPlaceDTOList[0].detailedAddress || '', // 发货地址
          consignorDate: this.object?.loadingTime || '', // 发货时间
          consigneeInfo: `${this.object?.unLoadingPlaceDTOList[this.object?.unLoadingPlaceDTOList.length - 1].appUserName}  ${
            this.object?.unLoadingPlaceDTOList[this.object?.unLoadingPlaceDTOList.length - 1].contractTelephone
          }`, // 收货信息
          consigneeDate: this.object?.unloadingTime || '', // 收货时间
          consigneeAddress: this.object?.unLoadingPlaceDTOList[this.object?.unLoadingPlaceDTOList.length - 1].detailedAddress || '', // 收货地址
          goodsName: this.object?.goodsInfoDTOList[0].goodsName || '', // 货物名称
          shippingType: '整车运输',
          consignmentVolume: `${this.object?.goodsInfoDTOList[0]?.weight || '-'}吨/${this.object?.goodsInfoDTOList[0]?.volume || '-'}方/${
            this.object?.goodsInfoDTOList[0]?.number || '-'
          }件`, //托运量
          transporterInfo: '', //运输方信息
          freightAmount: this.object?.total || '', // 订单运费金额（元)
          pre: this.object?.expenseDTOList?.filter((item: any) => item.expenseCode === 'PRE')[0].price || '', //预付
          rece: this.object?.expenseDTOList?.filter((item: any) => item.expenseCode === 'RECE')[0].price || '', // 到付
          back: this.object?.expenseDTOList?.filter((item: any) => item.expenseCode === 'BACK')[0].price || '', // 回单付
          lunarKnot: 0,
          total: this.object?.subtotal || '', // 合计（元)
          paymentTime: `到货后${this.object?.paymentDays || ''}天`, // 承诺支付运费时间
          year: new Date().getFullYear() || '', // 签约年份
          month: new Date().getMonth() + 1 || '', // 签约月份
          day: new Date().getDate() || '' // 签约日期
        }
      };
    } else if (this.types == 'bulk') {
      console.log('8888');
      params = {
        contractType: '1',
        resourceType: '2',
        signingObject: '1',
        templateType: 'MX',
        parametersDTO: {
          contractCode: '',
          shipperName: this?.shipperName || '', //托运人
          carrierName: this.object?.enterpriseInfoNamer ||  this.object?.enterpriseInfoName || '', //承运人
          shipperLegalPersonName: this.enterpriseInfo.legalPersonName || '', //托运法定代表人
          carrierLegalPersonName: this.enterpriseInfo.netLegalPersonName || '', //承运法定代表人
          consignorInfo: `${this.object.unLoadingPlaceDTOList[0]?.appUserName || ''}  ${
            this.object.unLoadingPlaceDTOList[0]?.contractTelephone || ''
          }`, // 发货信息
          consignorAddress: this.object.unLoadingPlaceDTOList[0].detailedAddress, // 发货地址
          consignorDate: '', // 发货时间
          consigneeInfo: `${this.object.unLoadingPlaceDTOList[this.object.unLoadingPlaceDTOList.length - 1]?.appUserName || ''}  ${
            this.object.unLoadingPlaceDTOList[this.object.unLoadingPlaceDTOList.length - 1]?.contractTelephone || ''
          }`, // 收货信息
          consigneeDate: '', // 收货时间
          consigneeAddress: this.object.unLoadingPlaceDTOList[this.object.unLoadingPlaceDTOList.length - 1]?.detailedAddress || '', // 收货地址
          goodsName: this.object.goodsInfoDTOList[0]?.goodsName || '', // 货物名称
          shippingType: '大宗运输',
          consignmentVolume: `${this.object?.goodsInfoDTOList?.[0]?.weight || '-'}吨/${
            this.object?.goodsInfoDTOList?.[0]?.volume || '-'
          }方/${this.object.goodsInfoDTOList?.[0]?.number || '-'}车`, //托运量
          transporterInfo: '', //运输方信息
          freightAmount: '', // 订单运费金额（元)
          pre: '', //预付
          rece: '', // 到付
          back: '', // 回单付
          lunarKnot: '',
          total: '', // 合计（元)
          paymentTime: `到货后${this.object?.paymentDays || ''}天`, // 承诺支付运费时间
          year: new Date().getFullYear() || '', // 签约年份
          month: new Date().getMonth() + 1 || '', // 签约月份
          day: new Date().getDate() || '' // 签约日期
        }
      };
    }
    if (params?.parametersDTO?.freightAmount) {
      params.parametersDTO.freightAmount = this.toThousands(params?.parametersDTO.freightAmount);
    }
    if (params?.parametersDTO?.pre) {
      params.parametersDTO.pre = this.toThousands(params?.parametersDTO.pre);
    }
    if (params?.parametersDTO?.rece) {
      params.parametersDTO.rece = this.toThousands(params?.parametersDTO.rece);
    }
    if (params?.parametersDTO?.back) {
      params.parametersDTO.back = this.toThousands(params?.parametersDTO.back);
    }
    if (params?.parametersDTO?.total) {
      params.parametersDTO.total = this.toThousands(params?.parametersDTO.total);
    }
    this.service.request(this.service.$api_getContractContent, params).subscribe(res => {
      if (res) {
        this.agreementContent = res;
      }
    });
  }

  toThousands(num: any) {
    let str = num.toString();
    return '￥' + str.replace(/(\d)(?=(?:\d{3})+$)/g, '$1,');
  }
}
